package com.sskj.uboex.ui.activity;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.just.agentweb.AgentWeb;
import com.sskj.common.widget.DensityWebView;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.uboex.R;
import com.sskj.uboex.bean.Notice;
import com.sskj.uboex.presenter.MsgDetailActivityPresenter;

import butterknife.BindView;

@Route(path = RConfig.APP_MSG_DETAIL)
public class MsgDetailActivity extends BaseActivity<MsgDetailActivityPresenter> {
    @Autowired
    String id;
    @BindView(R.id.webView)
    DensityWebView webView;
    @BindView(R.id.llWeb)
    LinearLayout llWeb;
    private AgentWeb mAgentWeb;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_msg_detail;
    }

    @Override
    public MsgDetailActivityPresenter getPresenter() {
        return new MsgDetailActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle("公告详情");
        initWeb();
    }

    @Override
    protected void initData() {
        mPresenter.getData(id);
    }

    public void updateUI(Notice datas) {
        String content = datas.getContent().replaceAll("<span.*?</span><span.*?</span>", "");
        content = "<font style=\"color:#1D2330;font-size:60px\">" + datas.getTitle() + "</font><br>\n" +
                "<div style=\"font-size:40px;text-align:right;margin-top:10px;color:#999999\">" + datas.getCreateTime() + "</div><br>"
                + "<hr size='0.1px',style=\"background-color:#7591b4\"></hr>"
                + "<div  style=\"font-size:40px;margin-top:1px;color:#1D2330\">" + content + "</div" +
                ">";
        content = "<body style=\"background:#ffffff;margin:0;padding-left:15px;padding-right:15px;margin-top:20px\">" + content + "</body" +
                ">";
        mAgentWeb.getUrlLoader().loadData(content, "text/html; charset=UTF-8", null);

    }

    public void initWeb() {
        ((ViewGroup) (webView.getParent())).removeView(webView);

        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(llWeb, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(Color.TRANSPARENT)// 使用默认进度条
                .setWebView(webView)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //注意这里开启 strict 模式 ， 设备低于 4.2 情况下回把注入的 Js 全部清空掉 ， 这里推荐使用 onJsPrompt 通信
                .createAgentWeb()
                .ready()
                .go("");
        WebSettings webSettings = mAgentWeb.getAgentWebSettings().getWebSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
    }


    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.i("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

        }
    };


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return true;
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }


}
